package com.nursinghome.emp.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.nursinghome.bean.RespBean;
import com.nursinghome.bean.Salary;
import com.nursinghome.emp.service.EmpSalaryService;
import com.nursinghome.utils.JwtUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;
import java.util.List;
import java.util.Map;

@CrossOrigin
@RestController
@RequestMapping("/emp/salary")
public class EmpSalaryController {
    @Autowired
    private EmpSalaryService empSalaryService;
    @GetMapping()
    public RespBean selectAllSalaryByMid(@RequestHeader(value = "token", required = false) String token,
                                         String data,
                                         Integer pageNum) {
        //判断令牌
        Map<String, Object> map = JwtUtils.parseJwtToMap(token);
        Integer jwtMid = (Integer) map.get("mid");
        if (jwtMid == null) {
            return RespBean.error("令牌信息不正确");
        }
        //时间转换
        if (data.length() > 0 && data != null) {
            ZonedDateTime zdt = ZonedDateTime.parse(data);
            // 加上八小时
            ZonedDateTime newZdt = zdt.plusHours(8);
            // 转换为字符串
            DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss.SSSXXX");
            data = newZdt.format(formatter);
        }
        //分页配置
        PageHelper.startPage(pageNum, 5);
        List<Salary> list = empSalaryService.selectByMid(data, jwtMid);
        PageInfo<Salary> pageInfo = new PageInfo<>(list);

        return RespBean.ok("",pageInfo);
    }
    //根据aid查询详细工资
    @GetMapping("/sid")
    public RespBean selectBySid(Long sid){
        Salary salary =  empSalaryService.selectBySid(sid);
        return RespBean.ok("",salary);
    }
}
